package cn.gbase.jiangsu.data.transfer.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.dao.QueryResult;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;

import cn.gbase.jiangsu.data.transfer.bean.Company;
import cn.gbase.jiangsu.data.transfer.bean.CompanyUser;
import cn.gbase.jiangsu.data.transfer.bean.ForumReply;
import cn.gbase.jiangsu.data.transfer.bean.ForumTopic;
import cn.gbase.jiangsu.data.transfer.bean.SupplierUser;
import cn.gbase.jiangsu.data.transfer.service.CompanyService;
import cn.gbase.jiangsu.data.transfer.service.ForumO2CService;
import cn.gbase.jiangsu.data.transfer.service.ForumS2OService;
import cn.gbase.jiangsu.data.transfer.service.SupplierService;
import cn.gbase.jiangsu.data.transfer.util.CheckAccessFilter;

/**
 * 论坛管理功能（供货商与运营商之间）.
 * 
 */
@IocBean
@At("/forumS2O")
@Filters(@By(type = CheckSession.class, args = { "supplierUser", "jsp:/supplierIndex" }))
public class ForumS2OAction {

	@Inject
	private ForumS2OService forumS2OService;

	@Inject
	private SupplierService supplierService;

	/**
	 * 分页查询主题（主题管理页面）.
	 * 
	 * @param currentPage
	 * @param groupId
	 * @param addTime
	 * @param keyword
	 * @param req
	 * @throws UnsupportedEncodingException
	 */
	@At("/listTopic")
	@Ok("jsp:/forumS2O/listTopic")
	public QueryResult listTopic(@Param("p") int currentPage,
			@Param("companyId") int companyId, @Param("title") String title,
			HttpServletRequest request, HttpSession session) {
		SupplierUser u = (SupplierUser) session.getAttribute("supplierUser");
		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("supplierId", "" + u.getSupplierId());
		itemMap.put("title", title);
		request.setAttribute("formdata", itemMap);
		request.setAttribute("fromId", u.getSupplierId());

		return forumS2OService.getTopicList(currentPage, u.getSupplierId(), title);
	}

	/**
	 * 跳转到查看主题页面（主题管理）.
	 */
	@At("/viewTopic")
	@Ok("jsp:/forumS2O/viewTopic")
	public void toViewTopic(@Param("topicId") int topicId,
			HttpServletRequest req) {

		// 主题
		ForumTopic topic = forumS2OService.getTopic(topicId);
		if (topic == null) {
			throw new RuntimeException("该主题不存在。");
		}
		req.setAttribute("topic", topic);

		// 查看次数+1
		forumS2OService.updateTopicClick(topicId, topic.getClick() + 1);

		// 取得回复列表
		List<ForumReply> replyList = forumS2OService.getReplyList(topicId);
		req.setAttribute("replyList", replyList);

	}

	/**
	 * 跳转到添加主题页面（主题管理）.
	 */
	@At("/toAddTopic")
	@Ok("jsp:/forumS2O/editTopic")
	public void toAddTopic(HttpServletRequest req, HttpSession session) {
		
	}

	/**
	 * 跳转到修改主题页面.
	 */
	@At("/toEditTopic")
	@Ok("jsp:/forumS2O/editTopic")
	public void toEditTopic(@Param("id") int id, HttpServletRequest req,
			HttpSession session) {

		// 要修改的主题
		ForumTopic p = forumS2OService.getTopic(id);
		req.setAttribute("formdata", p);

	}

	/**
	 * 保存主题.
	 * 
	 * @param sp
	 */
	@At("/saveTopic")
	@POST
	@Ok(">>:/forumS2O/listTopic")
	@Fail("jsp:/forumS2O/editTopic")
	@Aop("validationInterceptor")
	public void saveTopic(@Param("..") ForumTopic Topic,HttpSession session) {
		SupplierUser u = (SupplierUser) session.getAttribute("supplierUser");
		forumS2OService.addOrUpdTopic(Topic,u.getSupplierId());
	}

	/**
	 * 删除主题（逻辑删除）.
	 */
	@At("/delete")
	@Ok("json")
	public String delete(@Param("id") int id) {
		forumS2OService.deleteTopic(id);
		return "{id:0}";
	}

	/**
	 * 删除主题（逻辑删除）.
	 */
	@At("/deleteMany")
	@Ok("json")
	public String deleteMany(@Param("idStr") String idStr) {
		String[] idArray = idStr.split(",");
		for (String id : idArray) {
			forumS2OService.deleteTopic(Integer.valueOf(id));
		}
		return "{id:0}";
	}

	/**
	 * 保存回复.
	 * 
	 * @param sp
	 */
	@At("/saveReply")
	@POST
	@Ok(">>:/forumS2O/viewTopic?topicId=${obj}")
	public int saveReply(@Param("..") ForumReply reply,HttpSession session) {
		SupplierUser u = (SupplierUser) session.getAttribute("supplierUser");
		forumS2OService.addOrUpdateReply(reply,u.getSupplierId());
		return reply.getTopicId();
	}

}
